package com.example.myapplication.ui.dashboard;

import android.app.Fragment;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import android.widget.SimpleAdapter;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;

import android.app.FragmentManager;
import android.widget.TextView;
import android.widget.Toast;


import com.example.myapplication.R;
import com.example.myapplication.dao.HttpDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DashboardFragment extends Fragment {
    //private RelativeLayout myDrawerLayout;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        TextView textView = root.findViewById(R.id.novel_sort);
        EditText editText = root.findViewById(R.id.edittext);
        String[] mName = {"玄幻", "言情", "修真", "网游", "次元", "都市", "悬疑", "灵异"};
        ImageButton btnsearch = root.findViewById(R.id.ibt_search0);
        Handler handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.arg1 == 0)
                    Toast.makeText(getContext(), "Connect Wrong", Toast.LENGTH_SHORT).show();
                if (msg.arg1 == 1)
                    Toast.makeText(getContext(), "Connected", Toast.LENGTH_SHORT).show();
            }
        };
        btnsearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (editText.getText().toString().charAt(0) == '.') {
                    Toast.makeText(getContext(), "修改成功,等待连接。。。", Toast.LENGTH_SHORT).show();
                    HttpDao.urladd = "http://192.168" + editText.getText().toString() + ":8080/";
                    HttpDao httpDao = new HttpDao(handler);
                } else {
                    textView.setText(" ");
                    Bundle bundle = new Bundle();
                    bundle.putString("find", editText.getText().toString());
                    Fragment myfragment = new SortFragment();
                    myfragment.setArguments(bundle);
                    getFragmentManager().beginTransaction().replace(R.id.sort_container, myfragment).commit();
                }
            }
        });


        ListView list = (ListView) root.findViewById(R.id.list_sort);
        //初始化

        Bundle bundle = new Bundle();
        bundle.putString("sort", "玄幻");
        Fragment myfragment = new SortFragment();
        myfragment.setArguments(bundle);
        getFragmentManager().beginTransaction().replace(R.id.sort_container, myfragment).commit();
        textView.setText("小说类型:玄幻");

        ArrayAdapter adapter = new ArrayAdapter(getActivity(), R.layout.list_item, mName);
        list.setAdapter(adapter);
        list.isSelected();
        list.setOnItemClickListener((parent, view, position, id) -> {
            textView.setText("小说类型:" + ((TextView) view).getText().toString());
            Bundle bundle1 = new Bundle();
            bundle1.putString("sort", ((TextView) view).getText().toString());
            Fragment myfragment1 = new SortFragment();
            myfragment1.setArguments(bundle1);
            getFragmentManager().beginTransaction().replace(R.id.sort_container, myfragment1).commit();

        });
        return root;
    }

}
